package repository

import (
	"github.com/lab-online/internal/course/constant"
	"github.com/lab-online/internal/course/entity"
	"github.com/lab-online/pkg/database"
	"gorm.io/gorm"
)

type Repository struct {
	db *gorm.DB
}

var _ interface {
	SaveCourse(entity.CourseEntity) (entity.CourseEntity, error)
	DeleteCourse(entity.CourseEntity) error
	GetCourseByID(uint) (entity.CourseEntity, error)
	GetCourseWithExperiments(uint) (entity.CourseEntity, error)
	GetCourses(entity.CourseEntity) ([]entity.CourseEntity, error)
	CheckCourseExperimentExists(courseID, experimentID uint) (bool, error)

	SaveExperiment(entity.ExperimentEntity) (entity.ExperimentEntity, error)
	DeleteExperiment(entity.ExperimentEntity) error
	GetExperimentByID(uint) (entity.ExperimentEntity, error)
	SearchExperiments(entity.ExperimentEntity) ([]entity.ExperimentEntity, error)
} = &Repository{}

func New(postgres *gorm.DB) *Repository {
	return &Repository{postgres}
}

func errorHandler(err error) error {
	if err == nil {
		return nil
	}

	if database.IsRecordNotFound(err) {
		return constant.DBErrCourseNotFound
	}
	return constant.DBErrUnknown
}
